﻿namespace Luka;

/// <summary>
/// 集合扩展方法
/// </summary>
public static class IEnumerableExtensions
{
    /// <summary>
    /// 是否为集合
    /// </summary>
    /// <returns></returns>
    public static bool IsCollectionType(this Type type)
    {
        if (!type.GetGenericArguments().Any())
            return false;

        Type genericTypeDefinition = type.GetGenericTypeDefinition();
        var collectionTypes = new[] { typeof(IEnumerable<>), typeof(ICollection<>), typeof(IList<>), typeof(List<>) };
        return collectionTypes.Any(x => x.IsAssignableFrom(genericTypeDefinition));
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <returns></returns>
    public static List<T> ToPageList<T>(this IEnumerable<T> values, int PageIndex, int PageSize, ref int TotalNumber, ref int TotalPage) where T : class
    {
        TotalNumber = values.Count();
        TotalPage = (TotalNumber % PageSize) > 0 ? (TotalNumber / PageSize) + 1 : TotalNumber / PageSize;
        return values.Take(PageSize * (PageIndex - 1)).Take(PageSize).ToList();
    }
}